// pages/finance/withdraw/step1/step1.js
var util = require('../../../../utils/util.js');
var http = require('../../../../utils/http.js');
Page({
  // 监听银行卡号码输入
  watchCardNumber(event) {
    let cardNumber = event.detail.value;
    cardNumber = cardNumber.replace(/[\s]/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");//卡号四位分割一次
    this.setData({
      "bankcardForm.cardNumber": cardNumber
    })
    this.setData({
      "bankcardForm.card_number": util.trimAll(cardNumber)
    })
    if (util.isBankcard(cardNumber)) {
      let that = this;
      http.POST({
        url: 'api/publics/bankcard',
        data: that.data.bankcardForm,
        success: function(res){
          if(res.data.error===0){
            let result = res.data.data.result;
            let obj = Object.assign(result, that.data.bankcardForm);
            that.setData({
              'bankcardForm': obj
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      }) 
      this.setData({
        isActive: true
      })
    } else {
      this.setData({
        isActive: false
      })
    }
  },
   // 监听用户名输入
  watchCardHolder(event){
    let cardholder = event.detail.value;
    this.setData({
      "bankcardForm.cardholder": cardholder
    })
  },
  // 监听用户名输入支行名称
  watchBranchname(event) {
    let branchname = event.detail.value;
    this.setData({
      "bankcardForm.branchname": branchname
    })
  },
  //去添加银行卡并去提现步骤2
  toStep2(){
    let that = this;
    if (!this.data.bankcardForm.cardholder){
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.bankcardForm.branchname) {
      wx.showToast({
        title: '请输入开户行银行网点',
        icon: 'none'
      })
      return false;
    }
    http.POST({
      url: 'api/user/addBankCard',
      data: that.data.bankcardForm,
      success: function(res){
        if (res.data.error === 0) {
          wx.showToast({
            title: '添加银行卡成功',
            icon: 'none'
          });
          setTimeout(function(){
            wx.navigateTo({
              url: '../step2/step2',
            })
          },2000);
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    isActive: false,
    bankcardForm: {
      cardholder: '',
      cardNumber: '',//包含空格
      card_number: '',//去掉空格
      branchname: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})